#include "fileio.h"

//打开文件
FILE *openUsrFile(const char *path,const char *mode)
{
    FILE *file = NULL;
    if(NULL != path && NULL != mode)
    {
        file=fopen(path,mode);
        if(NULL == file)
        {
            printf("打开%s失败-->%s\n",path,strerror(errno));
            tuichu;
        }
    }
    else
    {
        printf("参数有误...\n");
        tuichu;
    }
    return file;
}

//读取信息
void loadInfoFromFile(const char *path,usrList *list)
{
    if(NULL == path || NULL == list)
    {
        return;
    }
    FILE *file=openUsrFile(path,"rb");
    usrNode *node = NULL;
    int ret=0;
    while (1)
    {
        node = mkNode();
        ret=fread(node,sizeof(usrNode),1,file);
        if(ret == 0)
        {
            free(node);
            node = NULL;
            if(feof(file))
            {
                break;
            }
            else if (ferror(file))
            {
                fclose(file);
                printf("读取信息失败\n");
                tuichu;
            }
        }
        node->pnext = NULL;
        myInsert(list,node);
    }
    fclose(file);
}

//保存信息
void saveListToFile(const char *path,usrList *list)
{
    if(NULL == path || NULL == list)
    {
        return;
    }
    FILE *file=openUsrFile(path,"wb");
    usrNode *node=list->pFirstNode;
    while(NULL != node)
    {
        fwrite(node,sizeof(usrNode),1,file);
        node = node->pnext;
    }
    fclose(file);
}

//将文件中的彩票信息读取放入到链表中
void loadLotteryInfoFromFile(const char *path,lyList *list)
{
    if(NULL == path || NULL == list)
    {
        return;
    }
    FILE *file=openUsrFile(path,"rb");
    lyNode *node = NULL;
    int ret=0;
    while (1)
    {
        node = mkLyNode();
        ret=fread(node,sizeof(lyNode),1,file);
        if(ret == 0)
        {
            free(node);
            node = NULL;
            if(feof(file))
            {
                break;
            }
            else if (ferror(file))
            {
                fclose(file);
                printf("读取信息失败\n");
                tuichu;
            }
        }
        node->pnext = NULL;
        lotteryInsert(list,node);
    }
    fclose(file);
}

//将彩票链表信息保存回文件
void saveLotteryListToFile(const char *path,lyList *list)
{
    if(NULL == path || NULL == list)
    {
        return;
    }
    FILE *file=openUsrFile(path,"wb");
    lyNode *node=list->pFirstNode;
    while(NULL != node)
    {
        fwrite(node,sizeof(lyNode),1,file);
        node = node->pnext;
    }
    fclose(file);
}

//将文件中购买的彩票信息读取放入到链表中
void loadBuyLotteryInfoFromFile(const char *path,buyLyList *list)
{
    if(NULL == path || NULL == list)
    {
        return;
    }
    FILE *file=openUsrFile(path,"rb");
    buyLyNode *node = NULL;
    int ret=0;
    while (1)
    {
        node = mkBuyLyNode();
        ret=fread(node,sizeof(buyLyNode),1,file);
        if(ret == 0)
        {
            free(node);
            node = NULL;
            if(feof(file))
            {
                break;
            }
            else if (ferror(file))
            {
                fclose(file);
                printf("读取信息失败\n");
                tuichu;
            }
        }
        node->pnext = NULL;
        buyLotteryInsert(list,node);
    }
    fclose(file);
}

//将购买彩票链表信息保存回文件
void saveBuyLotteryListToFile(const char *path,buyLyList *list)
{
    if(NULL == path || NULL == list)
    {
        return;
    }
    FILE *file=openUsrFile(path,"wb");
    buyLyNode *node=list->pFirstNode;
    while(NULL != node)
    {
        fwrite(node,sizeof(buyLyNode),1,file);
        node = node->pnext;
    }
    fclose(file);
}